package com.yanhui.utils.image;


import com.yanhui.modal.image.poster.QrCodeImage;
import com.yanhui.modal.image.poster.SmallImage;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class ImageUtil {
	/**
	 * 将一张图片写入另一张
	 * @param bigPath 大图片
	 * @param smallPath 小图片
	 * @param x 要下入图片与原图片的左边距（px）
	 * @param y 上边距（px）
	 * @param h 二维码高宽（px）
	 * @param outfile 输出文件
	 */
	public static final void overlapImage(String bigPath, String smallPath,int x,int y,int h,File outfile) {
		try {
			BufferedImage big = ImageIO.read(new File(bigPath));
			BufferedImage small = ImageIO.read(new File(smallPath));
			Graphics2D g = big.createGraphics();
			g.drawImage(small, x, y, h, h, null);
			g.dispose();
			ImageIO.write(big, "jpg", outfile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static final void overlapImage(BufferedImage big, final SmallImage[] smallImages, QrCodeImage[] qrCodeImage, File outfile, int flag) throws Exception{
		for(int i = 0 ; i < smallImages.length; i++){
			Graphics2D g = big.createGraphics();
			BufferedImage small = ImageIO.read(new File(smallImages[i].getSmallPath()));
			if(flag == 1){
				if(i == 0){
					//处理图片将其压缩成正方形的小图  
					small= scaleByPercentage(small, smallImages[i].getH(),smallImages[i].getH());  
					small = convertCircular(small);  
				}
			}
			g.drawImage(small, smallImages[i].getX(), smallImages[i].getY(), smallImages[i].getH(), smallImages[i].getH(), null);
			g.dispose();
		}
		for(int i = 0 ; i < qrCodeImage.length; i++){
			Graphics2D g = big.createGraphics();
			BufferedImage small = ImageIO.read(new File(qrCodeImage[i].getQrCodeFile()));
			g.drawImage(small, qrCodeImage[i].getX(), qrCodeImage[i].getY(), qrCodeImage[i].getH(), qrCodeImage[i].getH(), null);
			g.dispose();
		}
		ImageIO.write(big, "jpg", outfile);
	}
	
	
	/** 
     * 传入的图像必须是正方形的 才会 圆形  如果是长方形的比例则会变成椭圆的 
     * @param bi1 用户头像地址
     * @return 
     * @throws IOException 
     */  
    public static BufferedImage convertCircular(BufferedImage bi1) throws IOException{  
//       BufferedImage bi1 = ImageIO.read(new File(url));  
       //这种是黑色底的  
//       BufferedImage bi2 = new BufferedImage(bi1.getWidth(),bi1.getHeight(),BufferedImage.TYPE_INT_RGB);   
         
       //透明底的图片  
       BufferedImage bi2 = new BufferedImage(bi1.getWidth(),bi1.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);   
       Ellipse2D.Double shape = new Ellipse2D.Double(0,0,bi1.getWidth(),bi1.getHeight());  
       Graphics2D g2 = bi2.createGraphics();  
       g2.setClip(shape);   
       // 使用 setRenderingHint 设置抗锯齿     g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
       
     //开启抗锯齿  
       RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);  
       //使用高质量压缩  
       renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);  
       g2.setRenderingHints(renderingHints);       

       g2.drawImage(bi1,0,0,null);   
       //设置颜色  
       g2.setBackground(Color.green);  
       g2.dispose();  
       return bi2;  
    }  
    
    
    /** 
     * 缩小Image，此方法返回源图像按给定宽度、高度限制下缩放后的图像 
     * @param inputImage 
     * @param newWidth：压缩后宽度
     * @param newHeight：压缩后高度
     * @throws IOException
     * return  
     */  
    public static BufferedImage scaleByPercentage(BufferedImage inputImage, int newWidth, int newHeight) throws Exception {  
        //获取原始图像透明度类型  
        int type = inputImage.getColorModel().getTransparency();  
        int width = inputImage.getWidth();  
        int height = inputImage.getHeight();  
        //开启抗锯齿  
        RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);  
        //使用高质量压缩  
        renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);  
        BufferedImage img = new BufferedImage(newWidth, newHeight, type);  
        Graphics2D graphics2d =img.createGraphics();  
        graphics2d.setRenderingHints(renderingHints);          
        graphics2d.drawImage(inputImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null);  
        graphics2d.dispose();  
        return img;  
    }  
}
